package me.idashu.attg.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.idashu.attg.domain.CirlEntity;
import me.idashu.attg.domain.KeywordsEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.service.KeywordsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/keywords")
public class KeywordsController {
	
	private KeywordsService keywordsService;
	
	@Autowired
	public void setKeywordsService(KeywordsService keywordsService) {
		this.keywordsService = keywordsService;
	}
	
	@RequestMapping("/keywordsList")
	public String keywordsList(Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = keywordsService.keywordsCount();
		PageEntity pageEntity = new PageEntity();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		List<KeywordsEntity> keywordList = keywordsService.keywordsList(map);
		if(keywordList.size()<=0){
			keywordList = null;
		}
		model.addAttribute("listKeywords", "关键字列表");
    	model.addAttribute("keywordsList", keywordList);
    	model.addAttribute("pageEntity", pageEntity);
		return "keywords_list";
	}
	
	@RequestMapping("/deleteKeywords")
	public String deleteKeywords(@RequestParam("keywordsId") int keywordsId,Model model){
		keywordsService.deleteKeywords(keywordsId);
		keywordsList(model);
		return "keywords_list";
	}
	
	@RequestMapping("/keywordsAjaxList")
	public String keywordsAjaxList(Model model,@ModelAttribute PageEntity pageEntity){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = keywordsService.keywordsCount();
    	pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
    	map.put("pageEntity", pageEntity);
    	List<KeywordsEntity> keywordList = keywordsService.keywordsList(map);
    	model.addAttribute("listKeywords", "关键字列表");
    	model.addAttribute("keywordsList", keywordList);
    	model.addAttribute("pageEntity", pageEntity);
		return "keywords_list";
	}
	
	@RequestMapping("/addKeywords")
	public String addKeywords(Model model){
		model.addAttribute("addKeywords", "添加关键字");
		return "add_keywords";
	}
	
	@RequestMapping("/addKeywordsInfo")
	public String addKeywordsInfo(@ModelAttribute KeywordsEntity keywordsEntity,Model model){
		String keywords = keywordsEntity.getContent();
		int count = keywordsService.getKeywords(keywords.trim());
		if(count>0){
			model.addAttribute("success", "关键字已存在，请删除后再添加！");
			return "gonggao";
		}else{
			model.addAttribute("success", "添加关键字成功！");
			keywordsEntity.setTime(new Date());
			keywordsService.addKeywordsInfo(keywordsEntity);
		}
		return "success";
	}
	
	@RequestMapping("/updateKeywords")
	public String updateKeywords(@RequestParam("keywordsId") int keywordsId,Model model){
		String keywords = keywordsService.getKeywordsById(keywordsId);
		model.addAttribute("updateKeywords", "修改关键字");
		model.addAttribute("keywords", keywords);
		model.addAttribute("keywordsId", keywordsId);
		return "update_keywords";
	}
	
	@RequestMapping("/updateKeywordsInfo")
	public String updateKeywordsInfo(@ModelAttribute KeywordsEntity keywordsEntity,Model model){
		String keywords = keywordsEntity.getContent();
		int count = keywordsService.getKeywords(keywords.trim());
		if(count>0){
			model.addAttribute("success", "关键字已存在，请重新修改！");
			return "gonggao";
		}else{
			model.addAttribute("success", "修改关键字成功！");
			keywordsEntity.setTime(new Date());
			keywordsService.updateKeywordsInfo(keywordsEntity);
		}
		return "success";
	}
}
